package info.javafans.servlet;

import info.javafans.dao.CommentDao;
import info.javafans.domain.Comment;
import info.javafans.utils.DateTime;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class CommentServlet
 */
public class CommentServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CommentServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String action = request.getParameter("action");
		if (action == null) {
			return;
		}
		if(action.equals("add")) {
			this.doAdd(request, response);
		}
		
	}
	
	//添加评论
	void doAdd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
//		System.out.println("encoding:"+request.getCharacterEncoding());
		String author = request.getParameter("author"); 
		String email = request.getParameter("email");
		String postId = request.getParameter("postId");
		String content = request.getParameter("content");
//		System.out.println("content:"+content);
		Comment comment = new Comment();
		
		comment.setPostId(postId);
		comment.setAuthor(author);
		comment.setEmail(email);
		comment.setDate(DateTime.getNowTime());
		comment.setContent(content);
		
		CommentDao commentDao = new CommentDao();
		commentDao.addComment(comment);
		response.sendRedirect("ShowSinglePost?id="+postId);
	}

}
